Skip to content

fix(flatbuffers): use manual impl Default for struct object types#8947

Open
RenzoMXD wants to merge 4 commits intogoogle:masterfrom
RenzoMXD:fix/rust-derive-default-large-arrays
Open

fix(flatbuffers): use manual impl Default for struct object types#8947
RenzoMXD wants to merge 4 commits intogoogle:masterfrom
RenzoMXD:fix/rust-derive-default-large-arrays

Conversation

@RenzoMXD
Copy link

@RenzoMXD RenzoMXD commented Mar 3, 2026

Fix Rust codegen: use manual impl Default for struct object types

Problem

When a FlatBuffers struct contains a fixed-size array with more than 32 elements (e.g. [ubyte:64]), the generated Rust object API type uses #[derive(Default)] which fails to compile on Rust < 1.61, because Default was not implemented for [T; N] where N > 32 until that version.

Fix

Changed the Rust code generator (src/idl_gen_rust.cpp) to emit a manual impl Default for struct object types instead of
#[derive(Default)]. This matches the pattern already used for table object types.

The generated default values are:

  • Scalar/enum fields: use GetDefaultValue() (0, false, etc.)
  • Arrays of builtins: [0; N] (avoids the Default trait bound)
  • Arrays of structs/enums: ::flatbuffers::array_init(|_| Default::default())

Code generation changes

Changes were made to src/idl_gen_rust.cpp.

Test

Added LargeArrayStruct { d:[ubyte:64]; } to tests/arrays_test.fbs .

Closes #8943

@RenzoMXD RenzoMXD requested a review from dbaileychess as a code owner March 3, 2026 16:12
@google-cla
Copy link

google-cla bot commented Mar 3, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

switch (full_type) {
case ftArrayOfBuiltin: {
// e.g. [0; 64] for [u8; 64]
code_ += " {{FIELD}}: [0; " +
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a rust person nor do I use our generated code so take what I ask at face value :)

Does this work for arrays of bool?

@jtdavis777
Copy link
Collaborator

hey @RenzoMXD I think you need to run the script scripts/generate_code.py with the --flatc argument pointing to a copy of your compiled flatc.

@jtdavis777 jtdavis777 added the waiting-for-update This PR is waiting for a change from the author or contributors before it is ready for merge label Mar 5, 2026
@RenzoMXD
Copy link
Author

RenzoMXD commented Mar 5, 2026

hey @RenzoMXD I think you need to run the script scripts/generate_code.py with the --flatc argument pointing to a copy of your compiled flatc.

Hello, @jtdavis777 I regenerated and left commits. Please kindly review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c# c++ codegen Involving generating code from schema java json python rust swift waiting-for-update This PR is waiting for a change from the author or contributors before it is ready for merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Rust]: Object API codegen for arrays missing Default trait implementation

2 participants